package cn.dengta.webapp.im.entity;

import java.util.Date;
import java.util.regex.Pattern;

import cn.dengta.common.model.Status;
import cn.dengta.context.model.ImI18nKey;
import cn.dengta.webapp.base.entity.TimeBasedEntity;
import cn.dengta.webapp.user.model.AuditStatus;
import cn.dengta.webapp.user.vo.AudioVO;
import cn.dengta.webapp.user.vo.VideoVO;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.*;

/**
 * 用户自定义搭讪消息
 */
@Setter
@Getter
@Accessors(chain = true)
@TableName("im_user_greet_text")
public class UserGreetText extends TimeBasedEntity {

	public static final Pattern ENGLISH_SYMBOL = Pattern.compile("[\\w`~!@#$%^&* ()+=|{}':;\\[\\]<>/?！￥…—【】‘；：”“’。,.，、？]+");

	public static final int MAX_LIMIT = 3;
	public static final int MAX_LIMIT_CN = 10;
	public static final int DAY_UPDATE_MAX_LIMIT = 8;
	/** 允许上传的视频类型 */
	public static final String videoExts = ".mp4";
	/** 允许上传的音频类型 */
	public static final String audioExts = ".aac";

	public static final String[] GREET_CONFIG = { ImI18nKey.OFFICIAL_GREET, ImI18nKey.SELF_GREET };

	/** 用户ID */
	private Long userId;
	/** 搭讪内容 */
	private String content;
	/** 搭讪图 */
	private String image;
	/** 音频 */
	private String audio;
	/** 视频 */
	private String video;
	/** 排序 */
	private Integer sort;
	/** 是否使用（用于显示与隐藏切换） */
	private Integer isUse;
	/** 是否选中 */
	private Integer isSelect;
	/** 拒绝原因 */
	private String remark;
	/** 状态 */
	private Integer status;

	public UserGreetText init(UserGreetText input, Date now) {
		this.userId = X.expectNotNull(userId, input.userId);
		this.content = StringUtil.toString(input.content);
		this.image = StringUtil.toString(input.image);
		this.audio = StringUtil.toString(input.audio);
		this.video = StringUtil.toString(input.video);
		this.isUse = X.expectNotNull(input.isUse, isUse, 0);
		this.isSelect = X.expectNotNull(input.isSelect, isSelect, 0);
		this.sort = X.expectNotNull(input.sort, sort, 0);
		this.status = X.expectNotNull(input.status, status, Status.NO.value);
		super.initTime(now);
		return this;
	}

	public boolean canEdit() {
		return AuditStatus.VERIFY_FALSE.eq(getStatus()) || AuditStatus.VERIFY_TRUE.eq(getStatus());
	}

	public boolean pass() {
		return AuditStatus.VERIFY_TRUE.eq(getStatus());
	}

	@JSONField(serialize = false)
	public boolean isDefault() {
		return Status.asResult(isSelect);
	}

	public boolean use() {
		return Status.asResult(isUse);
	}

	public static final String USER_ID = "user_id";
	public static final String CONTENT = "content";
	public static final String IMAGE = "image";
	public static final String SORT = "sort";
	public static final String IS_USE = "is_use";
	public static final String IS_SELECT = "is_select";
	public static final String STATUS = "status";

	public GreetText toGreetText() {
		GreetText greetText = new GreetText();
		greetText.setId(id);
		greetText.setContent(content);
		greetText.setImage(image);

		if (StringUtil.notEmpty(audio)) {
			greetText.setAudioVO(JSONUtil.parseObject(audio, AudioVO.class));
		}

		if (StringUtil.notEmpty(video)) {
			greetText.setVideoVO(JSONUtil.parseObject(video, VideoVO.class));
		}
		return greetText;
	}

}
